package org.dashboard.client.widget.messageflow;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Node;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import org.dashboard.shared.Props;

import java.util.Arrays;

/**
 */
public class TodayMessageCountWidget extends SimplePanel {

    interface Binder extends UiBinder<Widget, TodayMessageCountWidget> {
    }

    private static final Binder binder = GWT.create(Binder.class);

    public TodayMessageCountWidget(String width, String height, int refreshInterval) {
        super();

        setWidget(binder.createAndBindUi(this));

        final Node node = getElement().getChild(0);
        DOM.setStyleAttribute((Element) node, "width", width);
        DOM.setStyleAttribute((Element) node, "height", height);

        final MessageFlowServiceAsync instance = (MessageFlowServiceAsync) GWT.create(MessageFlowService.class);

        final AsyncCallback<String[][]> statesAsyncCallback = new AsyncCallback<String[][]>() {
            public String[][] oldResult;

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(String[][] result) {
                if (!Arrays.deepEquals(result, oldResult)) {
                    Node rootNode = node.getChild(0).getChild(1);
                    String[] colNames = Props.instance().getMessageHeader();
                    StringBuilder sb = new StringBuilder();
                    sb.append("<div align=center style=\"-moz-box-flex:1;font-size:20px;text-align:center;font-weight:bold;\">Today<br></div>");
                    sb.append("<table WIDTH=100%><tr>");

                    for (int i = 0; i != 3; i++) {
                        sb.append("<td style=\"font-size: 25px;font-weight: bold;\">" + colNames[i] + "</td>");
                    }
                    sb.append("</tr>");
                    for (int i = 0; i != result.length; i++) {
                        String[] row = result[i];
                        sb.append("<tr>");
                        for (int y = 0; y != row.length - 1; y++) {
                            sb.append("<td style=\"font-size: 50px;font-weight: bold;\">" + row[y] + "</td>");
                        }
                        sb.append("</tr>");
                    }
                    sb.append("</table>");
                    sb.append("<div style=\"-moz-box-flex:1;font-size:20px;text-align:right;font-weight:bold;\">" + result[0][3] + "</div>");

                    ((Element) rootNode).setInnerHTML(sb.toString());
                }
                this.oldResult = result;
            }
        };
        final Timer timer = new Timer() {
            @Override
            public void run() {
                instance.getTodayMessagesHandledOrFailed(statesAsyncCallback);
            }
        };
        timer.scheduleRepeating(refreshInterval);

        instance.getTodayMessagesHandledOrFailed(statesAsyncCallback);
    }
}
